package com.wlr.study.leetcode;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;

/**
 * @author lerong.wang
 * @version 1.0.0
 * @description
 * @date 2024/12/17 10:14
 */
public class LeetCode1047 {
    public String removeDuplicates(String s) {
        StringBuilder stringBuilder = new StringBuilder();
        char[] charArray = s.toCharArray();
        Deque<Character> stack = new ArrayDeque<>();
        int i = 0;
        while (i < charArray.length) {
            if (!stack.isEmpty()&&charArray[i]==stack.peekLast()){
                stack.pollLast();
            }else{
                stack.addLast(charArray[i]);
            }
            i++;
        }
        while (!stack.isEmpty()){
            stringBuilder.append(stack.poll());
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        LeetCode1047 leetcode = new LeetCode1047();
        System.out.println(leetcode.removeDuplicates("abbaca"));
    }

}